package model;

import init.DBManagement;

import java.util.Map;
import java.util.Vector;

import model.beans.BibliographyBean;

public class Bibliography {

	public static Vector list(String str_academy, String str_subject){
		Vector vec_retVal = new Vector();
		String str_list = 
			" select BLI_TECH_KEY, "+
			"       TITULO,            " +
			"        AUTOR,           " +
			"        EDITORIAL,     " +
			"        ANIO,              " +
			"        ISBN,                " +
			"        ISSN,         " +
			"        TIPO                " +
			" from	 plablir "+
			" where  MATERIA_TK = '"+str_subject+"'";
		DBManagement database = new DBManagement();
	    database.executeQuery( str_list );
	    for( int int_i = 0; int_i < database.getNumRows(); int_i++){
	    	BibliographyBean bibliography = new BibliographyBean();
	    	bibliography.setId(Integer.parseInt(database.getItem(int_i, 0)));
	    	bibliography.setTitle(database.getItem(int_i, 1));
	    	bibliography.setAuthor(database.getItem(int_i, 2));
	    	bibliography.setEditorial(database.getItem(int_i, 3));
	    	bibliography.setYear(Integer.parseInt(database.getItem(int_i, 4)));
	    	bibliography.setIsbn(database.getItem(int_i, 5));
	    	bibliography.setIssn(database.getItem(int_i, 6));
	    	bibliography.setType(database.getItem(int_i,7));
	    	bibliography.setAcademy(str_academy);
	    	bibliography.setSubject(str_subject);
	    	vec_retVal.add(bibliography);
	    }
		return vec_retVal;
	}

	public static BibliographyBean instance( int int_bookId ){
		BibliographyBean biblio_retVal = null;
		String str_list = 
			" select  TITULO,      " +
			"        AUTOR,           " +
			"        EDITORIAL,     " +
			"        ANIO,              " +
			"        ISBN,                " +
			"        ISSN,         " +
			"        TIPO                " +
			" from	 plablir "+
			" where  BLI_TECH_KEY = '"+int_bookId+"'";
		DBManagement database = new DBManagement();
	    database.executeQuery( str_list );

	    if( database.getNumRows() == 1 ){
	    	biblio_retVal = new BibliographyBean();
	    	biblio_retVal.setTitle(database.getItem(0, 0));
	    	biblio_retVal.setAuthor(database.getItem(0, 1));
	    	biblio_retVal.setEditorial(database.getItem(0, 2));
	    	biblio_retVal.setYear(Integer.parseInt(database.getItem(0, 3)));
	    	biblio_retVal.setIsbn(database.getItem(0, 4));
	    	biblio_retVal.setIssn(database.getItem(0, 5));
	    	biblio_retVal.setType(database.getItem(0, 6));
	    }
	    database = null;
		return biblio_retVal;
	}
	
	public static int add(Map map_data, String str_subject){
		int int_add;
		String str_titulo = (String)map_data.get("titulo");
		String str_autor = (String)map_data.get("autor");
        String str_editorial  = (String)map_data.get("editorial");
        Integer anio = (Integer)map_data.get("anio");
        String str_isbn =  (String)map_data.get("isbn");
        String str_issn =  (String)map_data.get("issn");
        String str_tipo =  (String)map_data.get("tipo");
		String str_add =
            " INSERT INTO plablir ( "+
            "        MATERIA_TK, "+
            "        TITULO, "+
            "        AUTOR, "+
            "        EDITORIAL, "+
            "        ANIO,  "+
            "        ISBN, "+
            "        ISSN, "+
            "        TIPO  "+
            " ) VALUES ( "+
            "        '"+str_subject+"', "+
            "        '"+str_titulo+"', "+
            "        '"+str_autor+"'," +           
            "        '"+str_editorial+"'," +
            "        '"+anio+"'," +
            "        '"+str_isbn+"'," +
            "        '"+str_issn+"'," +
            "        '"+str_tipo +"'"+
            " ) ";
		DBManagement database = new DBManagement();
        boolean bol_insertStatus = database.executeQuery( str_add );
        int_add = bol_insertStatus ? 1 : 0;
        database = null;
	    return int_add;
	}
	
	public static int edit( Map map_data ){
		int int_edit;
		int int_id = ((Integer)map_data.get("id")).intValue();
		String str_titulo = (String)map_data.get("titulo");
		String str_autor = (String)map_data.get("autor");
        String str_editorial  = (String)map_data.get("editorial");
        Integer anio = (Integer)map_data.get("anio");
        String str_isbn =  (String)map_data.get("isbn");
        String str_issn =  (String)map_data.get("issn");
        String str_tipo =  (String)map_data.get("tipo");
		String str_edit =
            " UPDATE plablir "+
            " SET    TITULO = '"+str_titulo+"', "+
            "        AUTOR =  '"+str_autor+"',"+
            "        EDITORIAL = '"+str_editorial+"',"+
            "        ANIO = '"+anio+"',"+
            "        ISBN = '"+str_isbn+"',"+
            "        ISSN = '"+str_issn+"',"+
            "        TIPO = '"+str_tipo+"'"+
            " where  BLI_TECH_KEY = "+int_id;
		DBManagement database = new DBManagement();
        boolean bol_insertStatus = database.executeQuery( str_edit );
        int_edit = bol_insertStatus ? 1 : 0;
        database = null;
	    return int_edit;
	}
	
	public static int delete( int int_bookid ){
		int int_delete;
		
		String str_delete =
            " delete from plablir "+
            " where  BLI_TECH_KEY = "+int_bookid;
		DBManagement database = new DBManagement();
        boolean bol_insertStatus = database.executeQuery( str_delete );
        int_delete = bol_insertStatus ? 1 : 0;
        database = null;
	    return int_delete;
	}
}
